import 'package:flutter/material.dart';
import 'package:login/dao/login_dao.dart';
import 'package:login/extension/padding_extension.dart';
import 'package:login/util/navigator_util.dart';
import 'package:login/util/string_util.dart';
import 'package:login/widget/input_widget.dart';
import 'package:login/widget/login_button.dart';
import 'package:url_launcher/url_launcher.dart';

/// 登陆页
class LoginPage extends StatefulWidget {
  const LoginPage({Key? key}) : super(key: key);

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  bool loginEnable = false;

  String? userName;
  String? password;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      body: Stack(
        children: [
          ..._background(),
          _content(),
        ],
      ),
    );
  }

  _background() {
    return [
      Positioned.fill(child: Image.network("https://o.devio.org/images/bg_cover/robot.webp", fit: BoxFit.cover)),
      Positioned.fill(child: Container(decoration: const BoxDecoration(color: Colors.black54)))
    ];
  }

  _content() {
    return Positioned.fill(
        left: 25,
        right: 25,
        child: ListView(children: [
          const Text('ChatGPT', style: TextStyle(fontSize: 26, color: Colors.white)),
          Container(
              margin: const EdgeInsets.only(top: 140),
              child: InputWidget(
                  hint: '请输入账号',
                  onChange: (value) {
                    userName = value;
                    _checkBtnSts();
                  })),
          Container(
              margin: const EdgeInsets.only(top: 80),
              child: InputWidget(
                  hint: '请输入密码',
                  obscureText: true,
                  onChange: (value) {
                    password = value;
                    _checkBtnSts();
                  })),
          60.paddingHeight,
          LoginButton(title: '登陆', enable: loginEnable, onPressed: () => _login()),
          15.paddingHeight,
          Align(
              alignment: Alignment.centerRight,
              child: InkWell(
                  onTap: _tap2Register,
                  child: const Text(
                    '注册账号',
                    style: TextStyle(color: Colors.white),
                  )))
        ]));
  }

  _tap2Register() async {
    // 跳转到接口后台进行注册
    Uri uri = Uri.parse('https://api.devio.org/uapi/swagger-ui.html#/Account/registrationUsingPOST');
    if (await launchUrl(uri, mode: LaunchMode.externalApplication)) {
      debugPrint('不能打开这个外部应用的页面');
    }
  }

  _login() async {
    // 账号：5253  密码：5253
    try {
      await LoginDao.login('5253', '5253');
      debugPrint('登陆成功');
      if (context.mounted) {
        // ignore: use_build_context_synchronously
        NavigatorUtil.go2HomePage(context);
      }
    } catch (e) {}
  }

  _checkBtnSts() {
    bool enable;
    if (isNotEmpty(userName) && isNotEmpty(password)) {
      enable = true;
    } else {
      enable = false;
    }
    setState(() {
      loginEnable = enable;
    });
  }
}
